# -*- coding: utf-8 -*-

from PIL import Image
from PIL import ImageChops
import math
import operator
from functools import reduce
from PIL import  ImageFilter
def compare_differences(path_one, path_two, diff_save_location):
    """
    比较图片，如果有不同则生成展示不同的图片

    @参数一: path_one: 第一张图片的路径
    @参数二: path_two: 第二张图片的路径
    @参数三: diff_save_location: 不同图的保存路径
    """
    image_one = Image.open(path_one)
    image_two = Image.open(path_two)

    diff = ImageChops.difference(image_one, image_two)

    if diff.getbbox() is None:
        # 图片间没有任何不同则直接退出
        return
    else:
        diff.save(diff_save_location)

def compare_similar(img1, img2):
    """
     比较图片相似值

     """
    image1 = Image.open(img1)
    image2 = Image.open(img2)

    h1 = image1.histogram()
    h2 = image2.histogram()

    result = math.sqrt(reduce(operator.add,  list(map(lambda a,b: (a-b)**2, h1, h2)))/len(h1) )
    return result

def  extract_image_profile(imgpath, savepath):
    """
     提取图片轮廓

     """
    img = Image.open(imgpath)

    newimg2 = img.filter(ImageFilter.CONTOUR)
    newimg2.save(savepath)


if __name__ == '__main__':
    # compare_images('image/diff01.jpg',
    #                'image/diff02.jpg',
    #                'image/diff03.jpg')

    compare_differences('image/img1.jpg',
               'image/img2.jpg',
               'image/diff12.jpg')
    extract_image_profile("image/jiaju.jpg", "image/jiajulunkuo_m.jpg")
    extract_image_profile("image/jiaju_dengguang.jpg", "image/jiaju_dengguanglunkuo_m.jpg")